package com.andy.nature.ui.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.core.view.isVisible
import androidx.recyclerview.widget.RecyclerView
import com.andy.common.extend.setOnIMClickListener
import com.andy.image.ext.showAvatar
import com.andy.nature.databinding.AdapterMessageItemBinding
import com.andy.nature.model.TestBean
import com.andy.toast.extend.showNormalToast

/**
 * Author:Andy
 * Date:2023/7/19 19:25
 * Description:MessageAdapter
 */
class MessageAdapter : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
    private var list = mutableListOf<TestBean>()

    fun setData(data: List<TestBean>) {
        this.list.clear()
        this.list.addAll(data)
        notifyDataSetChanged()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return MessageViewHolder(AdapterMessageItemBinding.inflate(LayoutInflater.from(parent.context), parent, false))
    }

    override fun getItemCount(): Int {
        return list.size
    }


    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        (holder as MessageViewHolder).apply {
            bind(list[position])
        }
    }

    internal class MessageViewHolder(private val binding: AdapterMessageItemBinding) : RecyclerView.ViewHolder(binding.root) {
        fun bind(data: TestBean) {

            binding.apply {
                ivPhoto.showAvatar(data.avatar ?: "")
                tvTitle.text = data.name
                tvContent.text = data.content
                val num = data.unReadNum ?: 0
                tvRead.isVisible = num > 0
                tvRead.text = "$num"
                vRedDot.isVisible = data.showRed == true
                binding.rootView.setOnIMClickListener {
                    "${data.name}被点击了".showNormalToast()
                }
            }
        }
    }
}